#! /usr/bin/python

import tc
import random

def generate_neighborhood(directionmap):
	#LOAD TCs
	# These are put in arrays so a random selection can pick one of the variations of the same style
	# street styles
	street_ts = [tc.load("tc/street_t.tc")]
	street_xs = [tc.load("tc/street_x.tc")]
	street_corners = [tc.load("tc/street_corner.tc")]
	street_deadends = [tc.load("tc/street_deadend.tc")]
	street_straights = [tc.load("tc/street_straight.tc")]
	# house styles
	houses = [tc.load("tc/house1.tc")]
	corner_houses = [tc.load("tc/corner_house1.tc")]
	# yard styles
	yards = [tc.load("tc/yard.tc")]
	# parks
	parks = [tc.load("tc/lake.tc")]
	
	
	#GENERATE THE NEIGHBORHOOD (nbh)
	nbh = tc.new(125,125)
	
	#place the center
	if len(directionmap) == 4:
		#x
		nbh = tc.insert(nbh, rand(street_xs), 50, 50)
	elif len(directionmap) == 3:
		#t's
		if not ('S' in directionmap):
			nbh = tc.insert(nbh, rand(street_ts), 50, 50)
		if not ('E' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_ts), 1), 50, 50)
		if not ('N' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_ts), 2), 50, 50)
		if not ('W' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_ts), 3), 50, 50)
	elif len(directionmap) == 2:
		#straight through
		if ('N' in directionmap) and ('S' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_straights), 1), 50, 50)
		elif ('E' in directionmap) and ('W' in directionmap):
			nbh = tc.insert(nbh, rand(street_straights), 50, 50)
		#corners
		elif ('N' in directionmap) and ('E' in directionmap):
			nbh = tc.insert(nbh, rand(street_corners), 50, 50)
		elif ('N' in directionmap) and ('W' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_corners), 1), 50, 50)
		elif ('S' in directionmap) and ('W' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_corners), 2), 50, 50)
		elif ('S' in directionmap) and ('E' in directionmap):
			nbh = tc.insert(nbh, tc.rotate(rand(street_corners), 3), 50, 50)
	elif len(directionmap) == 1:
		#dead end / cul de sac
		if directionmap == 'N':
			nbh = tc.insert(nbh, rand(street_deadends), 50, 50)
		if directionmap == 'W':
			nbh = tc.insert(nbh, tc.rotate(rand(street_deadends), 1), 50, 50)
		if directionmap == 'S':
			nbh = tc.insert(nbh, tc.rotate(rand(street_deadends), 2), 50, 50)
		if directionmap == 'E':
			nbh = tc.insert(nbh, tc.rotate(rand(street_deadends), 3), 50, 50)

	#place the straight roads
	for d in list(directionmap):
		if d == 'N':
			nbh = tc.insert(nbh, tc.rotate(rand(street_straights), 1), 0, 50)
			nbh = tc.insert(nbh, tc.rotate(rand(street_straights), 1), 25, 50)
		if d == 'S':
			nbh = tc.insert(nbh, tc.rotate(rand(street_straights), 1), 75, 50)
			nbh = tc.insert(nbh, tc.rotate(rand(street_straights), 1), 100, 50)
		if d == 'E':
			nbh = tc.insert(nbh, rand(street_straights), 50, 75)
			nbh = tc.insert(nbh, rand(street_straights), 50, 100)
		if d == 'W':
			nbh = tc.insert(nbh, rand(street_straights), 50, 0)
			nbh = tc.insert(nbh, rand(street_straights), 50, 25)

	#place the corner houses
	if ('N' in directionmap) and ('W' in directionmap):
		nbh = tc.insert(nbh, rand(corner_houses), 25, 25)
	if ('S' in directionmap) and ('W' in directionmap):
		nbh = tc.insert(nbh, tc.rotate(rand(corner_houses), 1), 75, 25)
	if ('S' in directionmap) and ('E' in directionmap):
		nbh = tc.insert(nbh, tc.rotate(rand(corner_houses), 2), 75, 75)
	if ('N' in directionmap) and ('E' in directionmap):
		nbh = tc.insert(nbh, tc.rotate(rand(corner_houses), 3), 25, 75)

	#place the normal houses
	if 'N' in directionmap:
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 1), 0, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 3), 0, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 1), 0, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 3), 0, 100)
	else:
		nbh = tc.insert(nbh, rand(houses), 25, 25)
		nbh = tc.insert(nbh, rand(houses), 25, 50)
		nbh = tc.insert(nbh, rand(houses), 25, 75)
		nbh = tc.insert(nbh, rand(yards), 0, 25)
		nbh = tc.insert(nbh, rand(yards), 0, 50)
		nbh = tc.insert(nbh, rand(yards), 0, 75)
	if 'W' in directionmap:
		nbh = tc.insert(nbh, rand(houses), 25, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 2), 75, 0)
		nbh = tc.insert(nbh, rand(yards), 0, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 2), 100, 0)
	else:
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 1), 25, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 1), 50, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 1), 75, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 1), 25, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 1), 50, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 1), 75, 0)
	if 'S' in directionmap:
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 1), 100, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 3), 100, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 1), 100, 0)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 3), 100, 100)
	else:
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 2), 75, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 2), 75, 50)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 2), 75, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 2), 100, 25)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 2), 100, 50)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 2), 100, 75)
	if 'E' in directionmap:
		nbh = tc.insert(nbh, rand(houses), 25, 100)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 2), 75, 100)
		nbh = tc.insert(nbh, rand(yards), 0, 100)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 2), 100, 100)
	else:
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 3), 25, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 3), 50, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(houses), 3), 75, 75)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 3), 25, 100)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 3), 50, 100)
		nbh = tc.insert(nbh, tc.rotate(rand(yards), 3), 75, 100)

	#fix special case: corner street and dead end corners
	#corners
	if len(directionmap) == 2:
		if ('N' in directionmap) and ('E' in directionmap):
			nbh = tc.insert(nbh, rand(parks), 75, 0)
		elif ('N' in directionmap) and ('W' in directionmap):
			nbh = tc.insert(nbh, rand(parks), 75, 75)
		elif ('S' in directionmap) and ('W' in directionmap):
			nbh = tc.insert(nbh, rand(parks), 0, 75)
		elif ('S' in directionmap) and ('E' in directionmap):
			nbh = tc.insert(nbh, rand(parks), 0, 0)
	elif len(directionmap) == 1:
		if 'N' in directionmap:
			nbh = tc.insert(nbh, rand(parks), 75, 0)
			nbh = tc.insert(nbh, rand(parks), 75, 75)
		if 'S' in directionmap:
			nbh = tc.insert(nbh, rand(parks), 0, 0)
			nbh = tc.insert(nbh, rand(parks), 0, 75)
		if 'E' in directionmap:
			nbh = tc.insert(nbh, rand(parks), 0, 0)
			nbh = tc.insert(nbh, rand(parks), 75, 0)
		if 'W' in directionmap:
			nbh = tc.insert(nbh, rand(parks), 0, 75)
			nbh = tc.insert(nbh, rand(parks), 75, 75)
	
	#RETURN COMPILED nbh
	return nbh


#chooses a random element from the list (basically just an abbreviator)
def rand(l):
	return l[random.randint(0,len(l)-1)]

